package com.s7.recycler.helper;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.s7.recycler.adapter.BaseLoadAdapter;
import com.s7.recycler.listener.OnLoadDataListener;
import com.s7.recycler.listener.Recycler;

import java.util.List;

/**
 * Created by Administor on 2018\7\6 0006.
 * RecyclerView 上拉加载事件--只做加载动作
 * 兼容Grid布局、Linear布局、StaggeredGrid布局（支持布局切换）
 */
public class LoadHelper<T> implements SwipeRefreshLayout.OnRefreshListener, Recycler.OnLoadingListener {

    /**
     * adapter 存在的标志
     */
    private boolean hasInitAdapter = false;

    /**
     * 页码
     */
    private int page = 1;

    private boolean isLoad = false;

    /**
     * 页面是否不变
     */
    private boolean isLoading = false;
    private boolean isAadditional = true;
    private SwipeRefreshLayout mSwipeRefresh;
    private RecyclerView mRecycler;
    private OnLoadDataListener mLoadData;
    private BaseLoadAdapter<T> mAdapter;
    private Recycler.OnCheckedChangeListener<T> mCheckedChange;
    private Recycler.OnClickListener<T> mClick;
    private Recycler.OnItemClickListener<T> mItemClick;
    private Recycler.OnItemLongClickListener<T> mItemLongClick;

    /**
     * 设置是否显示附加页
     * @param aadditional
     */
    public void setAadditional(boolean aadditional) {
        isAadditional = aadditional;
    }

    /**
     * 设置 SwipeRefreshLayout
     * @param mSwipeRefresh
     */
    protected void setSwipeRefresh(SwipeRefreshLayout mSwipeRefresh) {
        this.mSwipeRefresh = mSwipeRefresh;
    }

    /**
     * 设置RecyclerView
     * @param mRecycler
     */
    protected void setRecycler(RecyclerView mRecycler) {
        this.mRecycler = mRecycler;
    }

    /**
     * 设置Adapter
     * @param mAdapter
     */
    protected void setAdapter(BaseLoadAdapter<T> mAdapter) {
        this.mAdapter = mAdapter;
    }

    protected void setLoadData(OnLoadDataListener mLoadData) {
        this.mLoadData = mLoadData;
    }

    public void setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
        this.mCheckedChange = mCheckedChange;
    }

    protected void setClick(Recycler.OnClickListener<T> mClick) {
        this.mClick = mClick;
    }

    protected void setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
        this.mItemClick = mItemClick;
    }

    protected void setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
        this.mItemLongClick = mItemLongClick;
    }

    /**
     * 初始化数据
     */
    public void initAdapter() {

        if (mAdapter == null || mRecycler == null) {
            return;
        }
        if(mSwipeRefresh != null && mLoadData != null) {
            mSwipeRefresh.setOnRefreshListener(this);
        }
        mAdapter.setAadditional(isAadditional)
                .setCheckedChange(mCheckedChange)
                .setClick(mClick)
                .setItemClick(mItemClick)
                .setItemLongClick(mItemLongClick);
        if (mLoadData != null) {
            mAdapter.setLoadingListener(this);
        }
        mRecycler.setAdapter(mAdapter);
        hasInitAdapter = true;
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        if (isLoad()) {
            return;
        }
        page = 1;
        isLoad = true;
        mLoadData.onLoadData(page);
    }

    /**
     * 重新加载
     */
    @Override
    public void onReload() {
    }

    /**
     * 加载更多/再试一次
     */
    @Override
    public void onLoading() {
        if (isLoad()) {
            return;
        }
        isLoad = true;
        mLoadData.onLoadData(page);
    }

    /**
     * 是否在执行加载动作（刷新/加载更多）
     * @return
     */
    private boolean isLoad() {
        if (isLoad) {
            setSwipeRefresh(false);
        }
        return isLoad;
    }

    /**
     * 设置刷新动画
     * @param flag
     */
    private void setSwipeRefresh(boolean flag) {
        if (mSwipeRefresh != null) {
            if (flag) {
                if (!mSwipeRefresh.isRefreshing()) {
                    mSwipeRefresh.setRefreshing(true);
                }
            } else {
                if (mSwipeRefresh.isRefreshing()) {
                    mSwipeRefresh.setRefreshing(false);
                }
            }
        }
    }

    /**
     * 切换RecyclerView 布局管理
     * @param layoutManager
     */
    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        if (mRecycler != null && mAdapter != null) {
            mRecycler.setLayoutManager(layoutManager);
            mAdapter.onAttachedToRecyclerView(mRecycler);
            notifyAdapter();
        }
    }

    /**
     * 回到第一页
     */
    public void loadFirstPage() {
        page = 1;
        if(!hasInitAdapter){
            initAdapter();
        }

        if (mLoadData != null) {
            isLoad = true;
            setSwipeRefresh(true);
            mLoadData.onLoadData(page);
        }
    }

    /**
     * 刷新Adapter
     * @param datas
     */
    public void notifyAdapter(List<T> datas) {
        if (page == 1) {
            setSwipeRefresh(false);
            mAdapter.onRefresh(datas);
        } else {
            mAdapter.addDatas(datas);
        }
        if (!isLoading) {
            isLoad = false;
            page++;
        }
    }

    /**
     * 刷新Adapter
     * @param data
     */
    public void notifyAdapter(T data) {
        mAdapter.addData(data);
    }

    /**
     * 刷新Adapter
     */
    public void notifyAdapter() {
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 网络连接失败
     */
    public void notifyError() {
        isLoad = false;
        mAdapter.setLoadError(true);
    }

    /**
     * 刷新所有数据
     * @param datas
     */
    public void notifyRefresh(List<T> datas) {
        mAdapter.onRefresh(datas);
    }

    /**
     * 替换指定位置数据
     * @param position
     * @param data
     */
    public void notifyReplace(int position, T data) {
        mAdapter.onReplace(position, data);
    }

    /**
     * 删除指定位置的内容
     * @param position
     */
    public void notifyRemove(int position) {
        mAdapter.onRemove(position);
    }

    /**
     * 获取所有数据
     * @return
     */
    public List<T> getData() {
        return mAdapter.getDatas();
    }

    /**
     * 添加数据是否改变页码（写入新的数据、但是页码不变）
     * @param loading
     */
    public void setLoading(boolean loading) {
        isLoading = loading;
    }

    public static class Builder<T> {
        private SwipeRefreshLayout mSwipeRefresh;
        private RecyclerView mRecycler;
        private BaseLoadAdapter<T> mAdapter;
        private Recycler.OnCheckedChangeListener<T> mCheckedChange;
        private Recycler.OnClickListener<T> mClick;
        private Recycler.OnItemClickListener<T> mItemClick;
        private Recycler.OnItemLongClickListener<T> mItemLongClick;
        private OnLoadDataListener mLoadData;
        private boolean isAadditional = true;

        /**
         * 设置是否显示附加页
         * @param isAadditional
         * @return
         */
        public Builder setAadditional(boolean isAadditional) {
            this.isAadditional = isAadditional;
            return this;
        }

        public Builder setSwipeRefresh(SwipeRefreshLayout mSwipeRefresh) {
            this.mSwipeRefresh = mSwipeRefresh;
            this.mSwipeRefresh.setColorSchemeColors(Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN);
            return this;
        }

        public Builder setSwipeRefresh(SwipeRefreshLayout mSwipeRefresh, int... colors) {
            this.mSwipeRefresh = mSwipeRefresh;
            this.mSwipeRefresh.setColorSchemeColors(colors);
            return this;
        }

        public Builder setRecycler(RecyclerView mRecycler, RecyclerView.LayoutManager layoutManager) {
            this.mRecycler = mRecycler;
            this.mRecycler.setLayoutManager(layoutManager);
            return this;
        }

        public Builder setRecycler(RecyclerView mRecycler, RecyclerView.LayoutManager layoutManager, RecyclerView.ItemDecoration itemDecoration) {
            this.mRecycler = mRecycler;
            this.mRecycler.setLayoutManager(layoutManager);
            this.mRecycler.addItemDecoration(itemDecoration);
            return this;
        }

        public Builder setAdapter(BaseLoadAdapter<T> mAdapter) {
            this.mAdapter = mAdapter;
            return this;
        }

        /**
         * 设置Adapter的底部是否显示加载更多
         * @param mAdapter
         * @param isLoad
         * @return
         */
        public Builder setAdapter(BaseLoadAdapter<T> mAdapter, boolean isLoad) {
            this.mAdapter = mAdapter;
            this.mAdapter.setLoad(isLoad);
            return this;
        }

        public Builder setLoadData(OnLoadDataListener mLoadData) {
            this.mLoadData = mLoadData;
            return this;
        }

        public Builder setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
            this.mCheckedChange = mCheckedChange;
            return this;
        }

        public Builder setClick(Recycler.OnClickListener<T> mClick) {
            this.mClick = mClick;
            return this;
        }

        public Builder setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
            this.mItemClick = mItemClick;
            return this;
        }

        public Builder setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
            this.mItemLongClick = mItemLongClick;
            return this;
        }

        public <T> LoadHelper<T> build() {
            LoadHelper mHelper = new LoadHelper<T>();
            mHelper.setAadditional(isAadditional);
            mHelper.setSwipeRefresh(mSwipeRefresh);
            mHelper.setRecycler(mRecycler);
            mHelper.setAdapter(mAdapter);
            mHelper.setLoadData(mLoadData);
            mHelper.setCheckedChange(mCheckedChange);
            mHelper.setClick(mClick);
            mHelper.setItemClick(mItemClick);
            mHelper.setItemLongClick(mItemLongClick);
            return mHelper;
        }
    }

}
